home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-06-07 | 3.1 KB | 138 lines | [TEXT/CWIE] |
- #include "Solution.h"
-
- #include "ProblemUtils.h"
-
- #include <stdio.h>
- #include <Files.h>
- #include <Errors.h>
-
- const int MAX_BOXES = 1000;
-
- static OSErr ReadUInt32FromHandle( Handle data, UInt32 *number )
- {
- OSErr err;
- char line[MAX_LINE_LEN];
- char *p;
-
- p = line;
- if ( ProblemReadLineFromHandle( data, line, MAX_LINE_LEN ) && ProblemGetUInt32( &p, number ) && *p == 0 ) {
- err = noErr;
- } else {
- err = -1;
- }
- return err;
- }
-
- static OSErr ReadPointFromHandle( Handle data, Point *pt )
- {
- OSErr err;
- char line[MAX_LINE_LEN];
- char *p;
- UInt32 number1, number2;
-
- p = line;
- if ( ProblemReadLineFromHandle( data, line, MAX_LINE_LEN )
- && ProblemGetUInt32( &p, &number1 )
- && ProblemGetUInt32( &p, &number2 ) && *p == 0 ) {
- pt->v = number1;
- pt->h = number2;
- err = noErr;
- } else {
- err = -1;
- }
- return err;
- }
-
- static OSErr ReadRectFromHandle( Handle data, Rect *r )
- {
- OSErr err;
- char line[MAX_LINE_LEN];
- char *p;
- UInt32 number1, number2, number3, number4;
-
- p = line;
- if ( ProblemReadLineFromHandle( data, line, MAX_LINE_LEN )
- && ProblemGetUInt32( &p, &number1 )
- && ProblemGetUInt32( &p, &number2 )
- && ProblemGetUInt32( &p, &number3 )
- && ProblemGetUInt32( &p, &number4 ) && *p == 0 ) {
- r->top = number1;
- r->left = number2;
- r->bottom = number3;
- r->right = number4;
- err = noErr;
- } else {
- err = -1;
- }
- return err;
- }
-
- pascal OSErr CheckPackBoxes( const FSSpec* infile, const FSSpec* outfile, Boolean *correct )
- {
- OSErr err;
- Handle indata;
- UInt32 box_count;
- UInt32 i, j;
- Rect frame;
- Point boxes[MAX_BOXES];
- Point boxes_saved[MAX_BOXES];
- Point toplefts[MAX_BOXES];
-
- *correct = false;
-
- err = ProblemFileRead( infile, &indata );
- if ( err == noErr ) {
- err = ReadRectFromHandle( indata, &frame );
- if ( err == noErr ) {
- err = ReadUInt32FromHandle( indata, &box_count );
- }
- if ( err == noErr ) {
- for ( i = 0; i < box_count; i++ ) {
- err = ReadPointFromHandle( indata, &boxes[i] );
- boxes_saved[i] = boxes[i];
- if ( err != noErr ) break;
- }
- }
-
- if ( err == noErr ) {
- PackBoxes( &frame, box_count, boxes, toplefts );
- *correct = true;
- for ( i = 0; i < box_count && *correct; i++ ) {
- Rect thisbox;
- if ( boxes_saved[i].h != boxes[i].h || boxes_saved[i].v != boxes[i].v ) {
- *correct = false;
- break;
- }
- thisbox.left = toplefts[i].h;
- thisbox.top = toplefts[i].v;
- thisbox.right = thisbox.left + boxes[i].h;
- thisbox.bottom = thisbox.top + boxes[i].v;
- if ( thisbox.left < frame.left || thisbox.top < frame.top
- || thisbox.right > frame.right || thisbox.bottom > frame.bottom ) {
- *correct = false;
- break;
- }
- for ( j = 0; j < box_count; j++ ) {
- if ( i != j ) {
- Rect thatbox;
- thatbox.left = toplefts[j].h;
- thatbox.top = toplefts[j].v;
- thatbox.right = thisbox.left + boxes[j].h;
- thatbox.bottom = thisbox.top + boxes[j].v;
- if ( thatbox.right <= thisbox.left
- || thatbox.left >= thisbox.right
- || thatbox.bottom <= thisbox.top
- || thatbox.top >= thisbox.bottom ) {
- } else {
- *correct = false;
- break;
- }
- }
- }
- }
- }
- DisposeHandle( indata );
- }
- return err;
- }
-